package com.bci.pwtz.service.umeng.impl;

import java.io.IOException;

import org.apache.commons.codec.digest.DigestUtils;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import com.bci.pwtz.common.PwtzConstants;
import com.bci.pwtz.common.remote.umeng.SendMsgResp;
import com.bci.pwtz.service.umeng.UMengAppUtilService;

public class UMengAppUtilServiceImpl implements UMengAppUtilService
{    
    
//    public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException
//    {
//        String retVal = "{\"ret\":\"FAIL\",\"data\":{\"error_code\":\"1000\"}}";
//        ObjectMapper mapper = new ObjectMapper();
//        SendMsgResp resp = mapper.readValue(retVal, SendMsgResp.class);
//    }
    @Override
    public SendMsgResp sendAndroidUnicast(String deviceID,String ticker,String title,String text,String afterOpen,String displayType) throws Exception 
    {
        AndroidUnicast unicast = new AndroidUnicast();
        unicast.setPredefinedKeyValue("appkey", PwtzConstants.UMENG_APP_KEY);
        String timestamp =  Integer.toString((int)(System.currentTimeMillis() / 1000));
        unicast.setPredefinedKeyValue("timestamp",timestamp);
        unicast.setPredefinedKeyValue("validation_token", DigestUtils.md5Hex(PwtzConstants.UMENG_APP_KEY.toLowerCase() + PwtzConstants.UMENG_APP_MASTER_SECRET.toLowerCase() + timestamp));

        unicast.setPredefinedKeyValue("device_tokens", deviceID);
        unicast.setPredefinedKeyValue("ticker", ticker);
        unicast.setPredefinedKeyValue("title",  title);
        unicast.setPredefinedKeyValue("text",  text);
        unicast.setPredefinedKeyValue("after_open", afterOpen);
        unicast.setPredefinedKeyValue("display_type", displayType);

        String retVal = unicast.send();
        ObjectMapper mapper = new ObjectMapper();
        return mapper.readValue(retVal, SendMsgResp.class);
    }

}
